//
//  TimeViewController.m
//  OneWordNew
//
//  Created by qianfeng on 15-7-2.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "TimeViewController.h"
#import "NewModel.h"
#import "NewCell.h"
#import "JHRefresh.h"
#import "MyWebViewController.h"
@interface TimeViewController ()
@end

@implementation TimeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatUI];
    [self firstDownLoad];
    [self creatRefreshView];
}

- (void)creatUI{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed: @"top"] forBarMetrics:UIBarMetricsDefault];
    self.automaticallyAdjustsScrollViewInsets=NO;
    self.dataArr=[[NSMutableArray alloc] init];
    self.tableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, kScreenSize.height-100) style:UITableViewStylePlain];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    [self.tableView registerNib:[UINib nibWithNibName:@"NewCell" bundle:nil] forCellReuseIdentifier:@"NewCell"];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    [self.view addSubview:self.tableView];
    
}
- (void)firstDownLoad{
    self.currentPage=0;
    _isload=NO;
    self.isRefreshing=NO;
    self.isLondMoring=NO;
    [self loadDataPage:self.currentPage isRefresh:NO];

}
- (void)loadDataPage:(NSInteger)page isRefresh:(BOOL)isRefresh{
    __weak typeof (self)weakSelf=self;
    NSString *url=[NSString stringWithFormat:kURLNew2,(NSInteger)self.currentPage];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // NSLog(@"%@",responseObject);
        if (responseObject) {
            if (_isRefreshing) {
                 [weakSelf.dataArr removeAllObjects];
            }
           
            
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            NSArray *data=dict[@"data"];
            for (NSDictionary *sub in data) {
                NewModel *model=[[NewModel alloc] init];
                [model setValuesForKeysWithDictionary:sub];
                [weakSelf.dataArr addObject:model];
            }
            [weakSelf.tableView reloadData];
            [weakSelf endRefreshing];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        [weakSelf endRefreshing];
    }];
    
    
}
- (void)creatRefreshView{
    __weak typeof(self)weakSelf=self;
    [self.tableView addRefreshHeaderViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isRefreshing) {
            return ;
        }
        weakSelf.isRefreshing=YES;
        weakSelf.currentPage=0;
        [weakSelf loadDataPage:weakSelf.currentPage isRefresh:YES];
        
    }];
    [self.tableView addRefreshFooterViewWithAniViewClass:[JHRefreshCommonAniView class] beginRefresh:^{
        if (weakSelf.isLondMoring) {
            return ;
        }
        weakSelf.isLondMoring=YES;
        weakSelf.currentPage+=20;
        weakSelf.isload=YES;
        [weakSelf loadDataPage:weakSelf.currentPage isRefresh:YES];
       
    }];
    
    
}
- (void)endRefreshing{
    if (self.isRefreshing) {
        self.isRefreshing=NO;
        [self.tableView headerEndRefreshingWithResult:JHRefreshResultSuccess];
    }
    if (self.isLondMoring) {
        self.isLondMoring=NO;
        [self.tableView footerEndRefreshing];
    }
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewCell *cell=[self.tableView dequeueReusableCellWithIdentifier:@"NewCell" forIndexPath:indexPath];
    NewModel *model=self.dataArr[indexPath.row];
    [cell showDataWith:model];
    return cell;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 232;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MyWebViewController *web=[[MyWebViewController alloc] init];
    NewModel *model=self.dataArr[indexPath.row];
    web.webUrl=model.absolute_url;
//    self.navigationController.navigationBarHidden=YES;
   // [self.tabBarController.tabBar setHidden:YES];
    web.idd=model.id;
    [self.navigationController pushViewController:web animated:YES];


}
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden=NO;

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
